package com.iaz.tech.tools.uml.file.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.behavioral.usecase.impl.NeuronRoler;
import com.iaz.tech.tools.uml.file.NeuronJSONTools;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 角色加载
 */
@EqualsAndHashCode(callSuper=false)
@Data
public class NeuronRolerLoader extends NeuronJSONTools{
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronRolerLoader.class);

    /**
     * 解析JSON
     */
    @Override
    public void parseJSON(IUMLNeuronAble umlObj, JSONObject json) {
       try {
            if (umlObj == null) {
                throw new Exception("umlObj 不能为空");
            }
            if (json == null) {
                throw new Exception("json 不能为空");
            }
            if (!(umlObj instanceof NeuronRoler)) {
                throw new Exception("umlObj 不是 NeuronRoler 类型"); 
            } 
            NeuronRoler roler = (NeuronRoler) umlObj;
            if (json.containsKey("name")) {
                roler.setName(json.getString("name"));
            } else {
                throw new Exception("name 必须输入");
            }
            if (json.containsKey("description")) {
                roler.setDescription(json.getString("description"));
            }        
       } catch(Exception e) {
            logger.error("出现异常：" + e.getMessage());
       }

    }

    @Override
    public JSONObject toJSON(IUMLNeuronAble objStruct) {
        JSONObject result = new JSONObject();
        try {
            if (objStruct == null) {
                throw new Exception("objStruct 不能为空");
            }
            if (!(objStruct instanceof NeuronRoler)) {
                throw new Exception("objStruct 不是 NeuronRoler 类型"); 
            } 
            NeuronRoler roler = (NeuronRoler) objStruct; 
            result.put("name", roler.getName());
            result.put("description", roler.getDescription()); 
        } catch(Exception e) {
            logger.error("出现异常：" + e.getMessage());
            result = new JSONObject();
        }
        return result;
    }

}
